unit clsVmoeda;

interface

uses Dm, dbtables, biblio;

type
  TVMoeda = class(TObject)
  private
    Moe_Codigo: string;
    VMoe_Data: TDateTime;
  public
    VMoe_Valor: Double;
    function Gravar(p_Moe_Codigo: string; p_VMoe_Data: TDateTime): Byte;
    function Recuperar(p_Moe_Codigo: string; p_VMoe_Data: TDateTime): Byte;
    function Recuperar_Ultimo: Double;
    function Excluir(p_Moe_Codigo: string; p_VMoe_Data: TDateTime): Byte;
    procedure Consultar(var p_q: TQuery; p_Moe_Codigo: string; p_VMoe_Data: TDateTime);
  end;

implementation

function TVMoeda.Gravar;
var
  tq: TQuery;
begin
  tq := Tquery.Create(DmInt);
  tq.databasename := dmint.Db.DatabaseName;
  tq.active := False;
  tq.sql.clear;
  tq.params.clear;
  tq.sql.add('SELECT * FROM VMoeda where Moe_Codigo=:k_Moe_Codigo and VMoe_Data=:k_VMoe_Data');
  tq.parambyname('k_Moe_Codigo').asstring := strzero(p_Moe_Codigo, 2);
  tq.parambyname('k_VMoe_Data').asdatetime := p_VMoe_Data;
  tq.open;
  if tq.eof then begin
    tq.sql.clear;
    tq.params.clear;
    tq.sql.add('INSERT INTO VMoeda VALUES(' +
      ':k_Moe_Codigo,' +
      ':k_VMoe_Data,' +
      ':k_VMoe_Valor)');
  end else begin
    tq.sql.clear;
    tq.params.clear;
    tq.sql.add('UPDATE VMoeda SET ' +
      'VMoe_Valor=:k_VMoe_Valor ' +
      'Where Moe_Codigo=:k_Moe_Codigo and VMoe_Data=:k_VMoe_Data');
  end;
  tq.parambyname('k_Moe_Codigo').asstring := strzero(p_Moe_Codigo, 2);
  tq.parambyname('k_VMoe_Data').asdatetime := p_VMoe_Data;
  tq.parambyname('k_VMoe_Valor').asfloat := VMoe_Valor;
  tq.close;
  tq.execsql;
  result := 1;
  tq.close;
  tq.Free;
end;

function TVMoeda.Recuperar;
var
  tq: TQuery;
begin
  tq := Tquery.Create(dmint);
  tq.databasename := DmInt.Db.DatabaseName;
  tq.active := False;
  tq.sql.clear;
  tq.sql.add('SELECT * FROM VMoeda where Moe_Codigo=:k_Moe_Codigo and VMoe_Data=:k_VMoe_Data');
  tq.parambyname('k_Moe_Codigo').asstring := strzero(p_Moe_Codigo, 2);
  tq.parambyname('k_VMoe_Data').asdatetime := p_VMoe_Data;
  tq.open;
  Moe_Codigo := p_Moe_Codigo;
  VMoe_Data := p_VMoe_Data;
  if tq.eof then begin
    Moe_Codigo := '';
    VMoe_Data := 0;
    VMoe_Valor := 0;
    Result := 0;
  end else begin
    Moe_Codigo := tq.fieldbyname('Moe_Codigo').asstring;
    VMoe_Data := tq.fieldbyname('VMoe_Data').asdatetime;
    VMoe_Valor := tq.fieldbyname('VMoe_Valor').AsFloat;
    Result := 1;
  end;
  tq.Close;
  tq.Free;
end;

function TVMoeda.Recuperar_Ultimo;
var
  tq: TQuery;
begin
  tq := Tquery.Create(dmint);
  tq.databasename := DmInt.Db.DatabaseName;
  tq.active := False;
  tq.sql.clear;
  tq.sql.add('SELECT vmoe_Valor FROM VMoeda where vmoe_data=(select max(vmoe_data) from vmoeda)');
  tq.open;
  result := tq.fieldbyname('vmoe_valor').asfloat;
  tq.close;
  tq.free;
end;

function TVMoeda.Excluir;
var
  tq: TQuery;
begin
  tq := Tquery.Create(dmint);
  tq.databasename := dmint.db.databasename;
  tq.active := False;
  tq.sql.Clear;
  tq.params.clear;
  tq.sql.add('DELETE FROM VMoeda where Moe_Codigo=:k_Moe_Codigo and VMoe_Data=:k_VMoe_Data');
  tq.parambyname('k_Moe_Codigo').asstring := strzero(p_Moe_Codigo, 2);
  tq.parambyname('k_VMoe_Data').asdatetime := p_VMoe_Data;
  tq.execSQL;
  Result := 1;
  tq.close;
  tq.Free;
end;

procedure TVMoeda.Consultar;
begin
  p_q.sql.Clear;
  p_q.Params.Clear;
  p_q.sql.add('SELECT * FROM VMoeda where Moe_Codigo=:k_Moe_Codigo and VMoe_Data>=:k_VMoe_Data order by VMoe_Data');
  p_q.parambyname('k_Moe_Codigo').asstring := strzero(p_Moe_Codigo, 2);
  p_q.parambyname('k_VMoe_Data').asdatetime := p_VMoe_Data;
end;

end.
